package com.foxmail.aroundme.mutlitype;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.appcompat.BuildConfig;
import android.support.v7.widget.RecyclerView;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import com.foxmail.aroundme.mutlitype.footer_header.FooterItem;
import com.foxmail.aroundme.mutlitype.footer_header.FooterProvider;
import com.foxmail.aroundme.mutlitype.footer_header.HeaderItem;
import com.foxmail.aroundme.mutlitype.footer_header.HeaderProvider;

import java.util.List;

/**
 * Created by gzl on 1/20/17.
 * <p>
 * 这层存储逻辑，数据增删改查放在这层
 */

public class GRecyclerAdapter extends MultiTypeAdapter implements IAdapterDataOperating, IAdapterViewOperating {

    private Context context;

    private LayoutInflater layoutInflater;

    //上次刷新位置
    private int mLastRefreshPosition = -1;

    //是否加载更多
    private boolean mShowFooter = false;



    public GRecyclerAdapter(@NonNull Context context, @NonNull List<BaseItem> data) {
        super(data);
        layoutInflater = LayoutInflater.from(context);
        init();
    }

    private void init() {
        //添加默认头和尾
        this.registerIndex(new FooterItem(), new FooterProvider(), MultiTypeAdapter.FOOTER);
    }

    @Override
    public void initData(List<BaseItem> data) {
        this.data = data;
        notifyDataSetChanged();
        showDataCount();
    }

    @Override
    public void addMoreData(List<BaseItem> data) {
        int startPos = data.size();
        this.data.addAll(data);
        notifyItemRangeInserted(startPos, data.size());
        showDataCount();
    }

    @Override
    public void addMoreDataToBottom(List data) {
        if (this.data.size() <= 0) {
            return;
        }
        this.data.addAll(this.data.size() - 1, data);
        notifyDataSetChanged();
        showDataCount();
    }

    @Override
    public void addMoreDataToTop(List data) {
        this.data.addAll(0, data);
        notifyDataSetChanged();
        showDataCount();
    }

    @Override
    public void addDataToIndex(int index, BaseItem t) {
        int size = data.size();
        if (index >= size) {
            data.add(t);
            notifyDataSetChanged();
            return;
        }
        data.add(index, t);
        notifyDataSetChanged();
        showDataCount();
    }

    @Override
    public List<BaseItem> getAllData() {
        return data;
    }

    @Override
    public BaseItem getDataItem(int position) {
        if (data.isEmpty()) {
            return null;
        }

        if (position > getDataCount()) {
            return null;
        }

        return data.get(position);
    }

    @Override
    public boolean isDataEmpty() {
        return data.isEmpty();
    }

    @Override
    public void deleteDataItem(int pos) {
        if (data.isEmpty()) {
            return;
        }
        int size = data.size();
        if (pos >= size) {
            return;
        }
        data.remove(pos);
        notifyDataSetChanged();
    }

    @Override
    public int getTipsLastRefreshPosition() {
        return mLastRefreshPosition;
    }

    @Override
    public void setTipsLastLocationPosition(int location) {
        mLastRefreshPosition = location;
    }

    @Override
    public void clearData() {
        if (isDataEmpty()) {
            return;
        }
        data.clear();
    }

    @Override
    public BaseItem getLastData() {
        if (!isDataEmpty()) {
            return data.get(data.size() - 1);
        }
        return null;
    }

    // TODO: 1/20/17  还要决策是不是要一直显示footer
    @Override
    public int getDataCount() {
        int i = mShowFooter ? 1 : 0;
        return data.size() + i;
    }



    @Override
    public void showFooter() {

    }

    @Override
    public void hideFooter() {

    }

    @Override
    public void showHeader() {

    }

    @Override
    public void hideHeader() {

    }

    //每次添加打印现在data数据
    private void showDataCount() {
        if (BuildConfig.DEBUG){
            Log.d("size", this.data.size() + "");
        }
    }
}
